package com.bridgeintelligent.tag.webserver.dim.mgmt.service.dim.del;

import com.bridgeintelligent.tag.mapper.DepartmentMapper;
import com.bridgeintelligent.tag.mapper.DimDelMapper;
import com.bridgeintelligent.tag.mapper.DimPropertyMapper;
import com.bridgeintelligent.tag.mapper.DimThemeMapper;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.*;
import com.bridgeintelligent.tag.webserver.dim.mgmt.service.catalog.model.DimDelModel;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by JackCheung on 2019/9/17 2:46 PM.
 */
@AllArgsConstructor
@Service
public class DelDimService implements IDelDimService {

    private DimDelMapper dimDelMapper;
    private DimPropertyMapper dimPropertyMapper;
    private DepartmentMapper departmentMapper;
    private DimThemeMapper dimThemeMapper;

    @Override
    public DimRep list(DimDelModel dimDelModel) {
        Page page = PageHelper.startPage(dimDelModel.getPageNumber(), dimDelModel.getPageSize());
        List<DimDataDel> list = dimDelMapper.findList(dimDelModel);
        Map<String, String> dataSourceMap = dataSourceMap();
        Map<String, String> departmentMap = departmentMap();
        Map<String, String> classifyMap = classifyMap();
        Map<String, String> themeMap = getThemeMap();
        list.forEach(dimDataDel -> {
            dimDataDel.setDataSourceCNName(dataSourceMap.get(dimDataDel.getDataSource()));
            //dimDataDel.setBusinessDepartmentCNName(departmentMap.get(dimDataDel.getBusinessDepartment()));
            dimDataDel.setSkillDepartmentCNName(departmentMap.get(dimDataDel.getSkillDepartment()));
            dimDataDel.setClassifyCNName(classifyMap.get(dimDataDel.getClassify()));
            //dimDataDel.setThemeCNName(themeMap.get(dimDataDel.getTheme()));
        });
        DimPage<DimDataDel> dimPage = new DimPage<>();
        dimPage.setContent(list);
        dimPage.setTotalPages(page.getPages());
        dimPage.setTotalElements(page.getTotal());
        dimPage.setSize(page.getPageSize());
        dimPage.setRequestPager(dimDelModel.getPageNumber());
        return new DimRep<>(dimPage, BaseCode.SUCCESS);
    }

    private Map<String, String> dataSourceMap() {
        return dimPropertyMapper.dataSourceList().stream().collect(Collectors.toMap(DimDataSource::getId, DimDataSource::getName));
    }

    private Map<String, String> classifyMap() {
        return dimPropertyMapper.classifyList().stream().collect(Collectors.toMap(DimClassify::getId, DimClassify::getName));
    }

    private Map<String, String> departmentMap() {
        return departmentMapper.list().stream().collect(Collectors.toMap(DimDepartment::getId, DimDepartment::getName));
    }

    private Map<String, String> getThemeMap() {
        return dimThemeMapper.list().stream().collect(Collectors.toMap(DimTheme::getId, DimTheme::getName));
    }
}
